home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / handson / Java / JBAdventure / AdvUtils.java < prev    next >
Encoding:
Java Source  |  1998-03-24  |  1.9 KB  |  59 lines

  1. import java.lang.*;
  2. import java.util.*;
  3. import java.io.*;
  4. public class AdvUtils extends java.lang.Object implements Serializable
  5. {
  6.     AdvUtils() {
  7.     }
  8.     // UTILITY METHODS FOR ADVENTURE GAME WRITERS
  9.  
  10.     // Vector Handling
  11.     
  12.     int ObNameAtIndex( String obName, Vector v ) {
  13.         // Try to find a Thing object with the name obName in the Vector v
  14.         // 
  15.         // Enumerate the objects in the Vector, v. If no match is
  16.         // made, return the default value -1. If a match is made
  17.         // return the index in v of the found object.
  18.         boolean found = false; // found = true if a match is made
  19.         Thing ob;             
  20.         int obIndex = -1;      // -1, the default, indicates no match is made
  21.         for (Enumeration e = v.elements(); e.hasMoreElements()&& !found; ) {
  22.             ob = (Thing)e.nextElement();
  23.             if (ob.getname().equalsIgnoreCase(obName)){ // case insensitive test
  24.                 found = true;
  25.                 obIndex = v.indexOf(ob);                                
  26.             }
  27.         }
  28.         return obIndex;
  29.     }        
  30.      
  31.     /*
  32.     // Here's an alternative implementation of the above method.
  33.     // The main differences are:
  34.     // a: it uses a while loop instead of a for loop
  35.     // b: it increments a counter, i, instead of using the indexOf() method
  36.     
  37.     int ObNameAtIndex( String ObName, Vector V ) {
  38.         Enumeration e = V.elements();
  39.         boolean found = false;
  40.         int i = -1;
  41.         while (e.hasMoreElements() && !found) {
  42.             if (((Thing)e.nextElement()).getname().equalsIgnoreCase(ObName)) 
  43.                 found = true; 
  44.             i++;
  45.                 
  46.         }
  47.         if (found)
  48.             return i;
  49.         else return -1;
  50.     }
  51.     */
  52.     
  53.     void TransferOb(Object Ob, Vector V1, Vector V2 ) {
  54.         // Move an object Ob from Vector V1 to Vector V2
  55.         V2.addElement(Ob);
  56.         V1.removeElement(Ob);
  57.     }
  58.  
  59. }